<?php

namespace App\Http\Middleware;

use App\AdminUser;
use Closure;

class CheckSession
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {

        // "aqj.one",
        $hostArr = [
            "aqj.one", "zdz.one", "app_promotion.local", "hgf.mobi"
        ];
        $host = $request->getHost();
        // 判断域名
        if (!in_array($host, $hostArr)) {
            if ($host == "dww.tw") {
                $url = "http://sina.lt";
                return redirect($url, 301);
            } else {
                return abort(404);
            }
        }


        if (!$request->session()->has('admin')) {
            return redirect('/login');
        }
        $admin = $request->session()->get('admin');
        if (!($admin instanceof AdminUser)) {
            return redirect('/login');
        }
        return $next($request);
    }
}
